package com.xgst.land.activity.check

import android.content.Intent
import android.view.View
import androidx.core.view.isGone
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.xgst.land.bean.QuerySampleBean
import com.xgst.land.databinding.ActivityBuildOrderLayoutBinding
import com.xgst.land.dialog.ProjectDialog
import com.xgst.land.dialog.TypeLandDialog
import com.xgst.land.viewmodel.BuildOrderViewModel

//创建盘点计划
class BuildInventoryOrderActivity:BaseActivity<ActivityBuildOrderLayoutBinding>(), View.OnClickListener, OnItemClickListener {
    override fun viewBinding(): ActivityBuildOrderLayoutBinding {
        return ActivityBuildOrderLayoutBinding.inflate(layoutInflater)
    }

    private var projectId = 0
    private var typeId = ""
    private var sampleBoxName = ""
    private var json = ""
    private lateinit var model:BuildOrderViewModel
    private val adapter = BuildAddAdapter()

    override fun init() {
        setTitle("创建盘点计划")
        model = ViewModelProvider(this)[BuildOrderViewModel::class.java]
        binding.tvProjectName.setOnClickListener(this)
        binding.tvSampleType.setOnClickListener(this)
        binding.imgSampleType.setOnClickListener(this)
        binding.btnStart.setOnClickListener(this)
        binding.btnPositive.setOnClickListener(this)
        binding.imgDelete.setOnClickListener(this)
        binding.cbAll.setOnClickListener(this)

        ClickUtils.expandClickArea(binding.imgDelete,30)

        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
    }


    override fun onClick(v: View?) {
        when(v){
            //项目
            binding.tvProjectName -> {
                ProjectDialog(this,projectId).show { id, name ->
                    binding.tvProjectName.text = name
                    projectId = id
                }
            }

            //样本类型
            binding.tvSampleType -> {
                TypeLandDialog(this).show { id, name ->
                    binding.tvSampleType.text = name
                    typeId = "$id"
                }
            }

            binding.cbAll -> {
                adapter.data.forEach {
                    it.isChecked = binding.cbAll.isChecked
                }
                adapter.notifyDataSetChanged()
            }

            //删除
            binding.imgDelete -> {
                if(adapter.data.removeIf { it.isChecked }){
                    adapter.notifyDataSetChanged()
                    showToast("已删除")
                }
            }

            //查询
            binding.btnStart -> {
                val intent = Intent(this,InventoryQueryActivity::class.java)
                intent.putExtra("projectId",projectId)
                intent.putExtra("sampleBoxName",sampleBoxName)
                intent.putExtra("sampleCategoryId",typeId)
                intent.putExtra("json",GsonUtils.toJson(adapter.data))
                startActivityForResult(intent){
                    json = it?.getStringExtra("json")?:""
                    val list = GsonUtils.fromJson<MutableList<QuerySampleBean.DataBean>>(json,genericType<MutableList<QuerySampleBean.DataBean>>())
                    if(list.size > 0 && binding.clRoot.isGone){
                        binding.clRoot.visibility = View.VISIBLE
                        binding.btnPositive.visibility = View.VISIBLE
                    }
                    adapter.addData(list)
                    binding.tvCheckCount.text = "${adapter.data.size}"
                }
            }

            binding.btnPositive -> {
                val name = binding.etName.text.toString()
                if(name.isEmpty()){
                    showToast("请输入表单名称")
                    return
                }
                var boxIds = ""
                adapter.data.forEach {
                    if(boxIds.isEmpty()){
                        boxIds = it.id.toString()
                    }else{
                        boxIds = "$boxIds,${it.id}"
                    }
                }
                showProgress()
                model.save(name,boxIds){
                    dismissProgress()
                    setResult(RESULT_OK)
                    finish()
                }
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as QuerySampleBean.DataBean
        item.isChecked = !item.isChecked
        adapter.notifyItemChanged(position)
        val list = adapter.data as MutableList<QuerySampleBean.DataBean>
        val bean = list.find { !it.isChecked }
        if(bean == null){
            binding.cbAll.isChecked = true
        }else{
            if(binding.cbAll.isChecked){
                binding.cbAll.isChecked = false
            }
        }
    }
}

